package com.dhms.vostok1.service.net


import com.dhms.vostok1.data.*
import com.dhms.vostok1.data.abnormals.SpotSource
import com.dhms.vostok1.data.danger_and_fault.DangerAndFaultCountData
import com.dhms.vostok1.data.danger_and_fault.DangerData
import com.dhms.vostok1.data.device_center.*
import com.dhms.vostok1.data.fault.*
import com.dhms.vostok1.data.maintenance.LubricationTaskData
import com.dhms.vostok1.data.maintenance.RepairData
import com.dhms.vostok1.data.message_center.MessageCenterData
import com.dhms.vostok1.data.message_center.MessageItemData
import com.dhms.vostok1.data.select_mbr.CompanyGroupBean
import com.dhms.vostok1.data.select_mbr.GroupMemberBean
import com.dhms.vostok1.data.spot_patrol.*
import com.dhms.vostok1.data.todo.ToDoCenterListResData
import com.dhms.vostok1.data.todo.ToDoStatisticsData
import okhttp3.MultipartBody
import retrofit2.Response
import retrofit2.http.*

private const val API_PREFIX_USER_V1 = "/api/user/v1"
private const val API_PREFIX_CUSTOMER_V1 = "/api/customer/v1"
private const val API_PREFIX_SPOT_PATROL = "spot-patrol"
private const val API_PREFIX_DEVICE_V1 = "/api/device/v1"
private const val API_PREFIX_V3 = "/api/v3"
private const val API_PREFIX_DEVICECU_V2 = "/api/devicecu/v2"
private const val API_PREFIX_FAULT_V2 = "/api/fault/v2"
private const val API_PREFIX_DEVICE_CU_V2 = "/api/devicecu/v2"
private const val API_PREFIX_DEVICE_CU_V3 = "/api/v3/devicecu"
private const val API_PREFIX_DEVICEHUB_V3 = "/api/v3/devicehub"
private const val API_PREFIX_MAINTAIN_V3 = "/api/v3/maintain"
private const val API_PREFIX_MESSAGE_CENTER_V3 = "/api/v3/message-center"

interface INetworkApi {

    @POST("$API_PREFIX_USER_V1/login")
    suspend fun login(@Body body: LoginReqBody): Response<LoginResData>

    @GET("$API_PREFIX_USER_V1/info/myself")
    suspend fun getUserInfo(): Response<UserInfoResData>

    @GET("$API_PREFIX_CUSTOMER_V1/info/myself")
    suspend fun getCustomerInfo(): Response<CustomerInfoResData>

    @POST("$API_PREFIX_USER_V1/code")
    suspend fun sendVerifyCode(@Body body: VerifyCodeReqBody): Response<VerifyCodeResData>

    @POST("$API_PREFIX_USER_V1/code/check")
    suspend fun checkVerifyCode(@Body body: CheckVerifyCodeReqBody): Response<CheckVerifyCodeResData>

    @POST("$API_PREFIX_USER_V1/modify/password")
    suspend fun resetPassword(@Body body: ResetPasswordReqBody): Response<ResetPasswordResData>

    @POST("$API_PREFIX_USER_V1/message_login")
    suspend fun messageLogin(@Body body: MessageLoginReqBody): Response<LoginResData>

    @GET("abnormal-records")
    suspend fun getAbnormalRecords(): Response<BaseResData<List<MockAbnormalRecords?>>>

    /*设备相关*/
    @GET("$API_PREFIX_DEVICE_V1/category/list")
    suspend fun getDeviceCategory(): Response<BaseResData<List<DeviceCategoryData>>>

    @GET("$API_PREFIX_V3/devicehub/category-devices")
    suspend fun getCategoryDevices(): Response<CompanyCategoyResData>

    /**
     * Device Center
     */
    @GET("$API_PREFIX_DEVICE_CU_V2/list")
    suspend fun getDeviceList(@QueryMap query: Map<String, String>): Response<BaseResData<DeviceListResData>>

    @GET("$API_PREFIX_DEVICE_CU_V3/realtime-params/{de_id}")
    suspend fun getDeviceMonitorInfo(@Path("de_id") deId: String): Response<BaseResData<DeviceMonitorResData>>

    @GET("/api/dynamic/v1/monitor")
    suspend fun getDeviceDynamicMonitorInfo(@QueryMap query: Map<String, String>): Response<BaseResData<DeviceDynamicMonitorResData>>

    @GET("/api/v3/statistic/device/state")
    suspend fun getDeviceStateStatisticData(): Response<BaseResData<DeviceStateStatisticResData>>

    @GET("$API_PREFIX_DEVICECU_V2/detail")
    suspend fun getDeviceDetail(
        @Query("de_id") de_id: Long
    ): Response<BaseResData<DeviceDetailData>>

    /**
     * Fault List
     */
    @GET("/api/fault/v2/list")
    suspend fun getFaultList(@QueryMap query: Map<String, String>): Response<BaseResData<FaultListResData>>

    @POST("$API_PREFIX_FAULT_V2/do")
    suspend fun sendFaultProcess(@Body body: FaultProcessReqBody): Response<BaseResData<Any>>

    @GET("/api/fault/v1/level/list")
    suspend fun getFaultLevelList(): Response<BaseResData<FaultLevelListData>>

    @GET("/api/v3/statistic/fault/level")
    suspend fun getFaultStatisticData(
        @QueryMap queryMap: Map<String, String>,
        @Query("state[]") state1: String,
        @Query("state[]") state2: String,
        @Query("state[]") state3: String,
        @Query("usetypes[]") userType1: String,
        @Query("usetypes[]") userType2: String,
    ): Response<BaseResData<FaultStatisticResData>>

    /**
     * Fault Detail
     */
    @GET("/api/fault/v1/info")
    suspend fun getFaultDetail(@Query("fa_id") fa_id: Long): Response<BaseResData<FaultDetail>>

    /**
     * Fault diagnose
     */
    @GET("/api/v3/smart-diagnose/libs")
    suspend fun getFaultDiagnoseInfo(@QueryMap query: Map<String, String>): Response<BaseResData<DiagnoseListResData>>

    /**
     * Related faults
     */
    @GET("/api/fault/v2/related")
    suspend fun getRelatedFaults(@QueryMap query: Map<String, String>): Response<BaseResData<RelatedFaultResData>>

    /**
     * 设备搜索中心 搜索的一级目录
     */
    @GET("$API_PREFIX_DEVICEHUB_V3/toplevel-category-stat")
    suspend fun getTopLevelCategory(): Response<BaseResData<List<TopLevelCategoryData>>>


    /**
     * 告警类别
     */
    @GET("/api/v3/smart-diagnose/fault-types")
    suspend fun getFaultTypes(@QueryMap query: Map<String, Long>): Response<BaseResData<List<FaultTypeItemData>>>

    /**
     * 点巡检
     */
    @GET("/api/v3/spot-patrol/task-groups")
    suspend fun getTaskGroups(
        @Query("offset") offset: Int?,
        @Query("limit") limit: Int?,
        @Query("query_task_type") query_task_type: String? = null,
        @Query("query_task_status") query_task_status: String? = null,
        @Query("end_time_greater") end_time_greater: Long? = null,
        @Query("is_remove_complete") is_remove_complete: Boolean? = false,
    ): Response<BaseResData<TaskGroupsResData>>

    @GET("/api/v3/spot-patrol/task/{group_id}/{task_id}")
    suspend fun getTaskDetail(
        @Path("group_id") group_id: String,
        @Path("task_id") task_id: Long
    ): Response<TaskResData>

    @PUT("/api/v3/spot-patrol/task")
    suspend fun updateTask(@Body body: TaskUpdateReqBody): Response<BaseResData<Any>>

    @PUT("/api/v3/spot-patrol/task-group/executors/{group_id}")
    suspend fun updateGroupExecutors(
        @Path("group_id") group_id: String,
        @Body body: TaskExecutorsReqBody
    ): Response<BaseResData<Any>>

    @GET("/api/v3/spot-patrol/task-group/{group_id}")
    suspend fun getTaskGroupDetail(@Path("group_id") group_id: String): Response<TaskGroupResData>

    // 获取任务列表
    @GET("/api/v3/spot-patrol/tasks")
    suspend fun getTaskList(@QueryMap query: Map<String, String>): Response<BaseResData<SecondaryBaseListData<TaskData>>>

    /*异常记录*/
    @GET("/api/v3/devicehub/abnormals")
    suspend fun getAbnormalItemList(
        @Query("page") page: Int,
        @Query("per_page") per_page: Int,
        @Query("start_time") start_time: Long? = null,
        @Query("end_time") end_time: Long? = null,
        @Query("device_ids") device_ids: String? = null,
        @Query("status") status: String? = null,
        @Query("source") source: SpotSource? = null,
    ): Response<AbnormalsResData>

    @POST("/api/v3/devicehub/abnormals")
    suspend fun createAbnormalItem(@Body body: AbnormalsCreateReqBody): Response<BaseResData<Any>>

    @GET("/api/v3/devicehub/abnormals/{id}")
    suspend fun getAbnormalDetail(
        @Path("id") id: String
    ): Response<AbnormalDetailResData>

    @POST("/api/v3/devicehub/abnormals/{id}")
    suspend fun updateAbnormalDetail(
        @Path("id") id: String,
        @Body body: AbnormalsUpdateReqBody
    ): Response<BaseResData<Any>>

    @GET("/api/v3/devicehub/spot/abnormal-items")
    suspend fun getAbnormalItems(
        @Query("page") page: Int,
        @Query("per_page") per_page: Int = 40,
    ): Response<AbnormalItemsResData>

    @POST("/api/v3/devicehub/spot/abnormal-items")
    suspend fun createAbnormalItems(
        @Body body: AbnormalItemsReqBody
    ): Response<BaseResData<Any>>

    @DELETE("/api/v3/devicehub/spot/abnormal-items/{id}")
    suspend fun deleteAbnormalItems(
        @Path("id") id: String,
    ): Response<BaseResData<Any>>

    // ============== 通用接口 ================

    /**
     * TSDB
     */
    @POST("/api/tsdb/v1/query")
    suspend fun getTSDBData(@Body body: TSDBReqBody): Response<BaseResData<TSDBResData>>

    /**
     * oss
     */
    @POST("/oss/file")
    suspend fun uploadFileToOSS(
        @Body body: MultipartBody,
    ): Response<BaseResData<UploadFileResData>>

    /*
    * Lubrication
    * */

    @GET("$API_PREFIX_MAINTAIN_V3/lubrication/tasks")
    suspend fun getLubricationTaskItems(@QueryMap query: Map<String, String>): Response<BaseResData<PrimaryBaseListData<LubricationTaskData>>>

    @GET("$API_PREFIX_MAINTAIN_V3/lubrication/task/{task_id}")
    suspend fun getLubricationTaskDetail(@Path("task_id") taskId: String): Response<BaseResData<LubricationTaskData>>

    @PUT("$API_PREFIX_MAINTAIN_V3/lubrication/task")
    suspend fun updateLubricationTaskDetail(@Body body: LubricationTaskData): Response<BaseResData<String>>

    /*
    * Maintenance
    * */
    @GET("$API_PREFIX_MAINTAIN_V3/maintenance/tasks")
    suspend fun getMaintenanceTaskItems(@QueryMap query: Map<String, String>): Response<BaseResData<PrimaryBaseListData<LubricationTaskData>>>

    @GET("$API_PREFIX_MAINTAIN_V3/maintenance/task/{task_id}")
    suspend fun getMaintenanceTaskDetail(@Path("task_id") taskId: String): Response<BaseResData<LubricationTaskData>>

    @PUT("$API_PREFIX_MAINTAIN_V3/maintenance/task")
    suspend fun updateMaintenanceTaskDetail(@Body body: LubricationTaskData): Response<BaseResData<String>>


    /**
     * Message Center
     */

    @GET("$API_PREFIX_MESSAGE_CENTER_V3/message/unread")
    suspend fun getMessageCenterUnread(): Response<BaseResData<PrimaryBaseListData<MessageCenterData>>>

    @GET("$API_PREFIX_MESSAGE_CENTER_V3/message/total")
    suspend fun getMessageUnReadCount(): Response<BaseResData<MessageTotalResData?>>

    @GET("$API_PREFIX_MESSAGE_CENTER_V3/messages")
    suspend fun getMessageCenterList(@QueryMap query: Map<String, String>): Response<BaseResData<PrimaryBaseListData<MessageItemData>>>

    @PUT("$API_PREFIX_MESSAGE_CENTER_V3/message-is-read/{message_id}")
    suspend fun messageIsRead(@Path("message_id") messageId: Int): Response<BaseResData<String>>

    @GET("$API_PREFIX_MAINTAIN_V3/repair/tasks")
    suspend fun getRepairTaskList(@QueryMap query: Map<String, String>): Response<BaseResData<PrimaryBaseListData<RepairData>>>


    /**
     * 点检仪数据上报
     */
    // 上传设备信息
    @POST("/api/v3/iotbridge/terminal-info")
    suspend fun uploadTerminalInfo(@Body body: ReportTerminalInfo): Response<BaseResData<Map<String, Any>?>>

    // 上传采集参数
    @POST("/api/v3/iotbridge/ts-data")
    suspend fun uploadTerminalTSData(@Body body: ReportTsData): Response<BaseResData<Map<String, Any>?>>

    // 上传震动数据
    @POST("/api/v3/iotbridge/block-data")
    suspend fun uploadTerminalBlockData(@Body body: ReportBlockData): Response<BaseResData<Map<String, Any>?>>

    /**
     * 隐患
     */
    @GET("/api/v3/damage/summary/perils")
    suspend fun getDangerList(@QueryMap query: Map<String, String>): Response<BaseResData<PrimaryBaseListData<DangerData>>>

    @GET("/api/v3/damage/app/total")
    suspend fun getDangersAndFaultsCount(): Response<BaseResData<DangerAndFaultCountData>>

    /*
     * Check Instrument Speed Relationships
     */
    @GET("/api/v3/spot-patrol/speed-rpm/param")
    suspend fun getCheckInstrumentSpeedSheetData(): Response<BaseResData<List<CheckInstrumentSpeedResData>>>

    /**
     * 振动分析
     */
    @POST("/signal/analyse")
    suspend fun vibAnalyse(@Body body: VibAnalyseReqBody): Response<Any>

    /**
     * 物联固件查询
     */
    @GET("/api/v3/iotconsole/firmwares")
    suspend fun getFirmwares(@QueryMap query: Map<String, String>): Response<BaseResData<PrimaryBaseListData<FirmwareData>>>

    /**
     * 获取公司层级信息
     */
    @GET("/api/v3/org/v3/org-hierarchy")
    suspend fun getCompanyGroup(): Response<BaseResData<List<CompanyGroupBean>>>

    /**
     * 获取公司成员
     */
    @GET("/api/v3/org/v3/{id}/members")
    suspend fun getMember(
        @Path("id") id: String,
        @Query("view_sub_org") view_sub_org: String? = null,
        @Query("keyword") keyword: String? = null
    ): Response<BaseResData<GroupMemberBean>>


    /**待办统计 **/
    @GET("/api/v3/processmanage/todocenter/statistics/count")
    suspend fun getToDoStatistics(): Response<BaseResData<ToDoStatisticsData>>

    /**待办 数据 列表*/
    @GET("/api/v3/processmanage/todocenter/matter")
    suspend fun getToDoCenterList(@QueryMap query: Map<String, String>): Response<BaseResData<ToDoCenterListResData>>

    @GET("/api/v3/damage/statistic/device/{device_id}")
    suspend fun getDamageStatisticDevice(@Path("device_id") deviceId: Long): Response<BaseResData<DevicePointListResData>>

}